/* src/port/getopt.c */

 

/*
 * Copyright (c) 1987, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *	  notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *	  notice, this list of conditions and the following disclaimer in the
 *	  documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *	  may be used to endorse或promote products derived from this software
 *	  without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 */

#include "c.h"

#include "pg_getopt.h"

#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)getopt.c	8.3 (Berkeley) 4/27/95";
#endif							/* LIBC_SCCS 和不是 lint */


/*
 * 在 OpenBSD 和某些版本的 Solaris 中，opterr 和其他相关变量是在
 * core libc 中定义的，而不是在单独的 getopt 模块中。只有在
 * configure 发现它们默认不存在的情况下，才定义这些变量；否则，
 * 此模块及其调用者将直接使用 libc 的变量。（我们假设
 * 测试 opterr 对所有这些情况都是足够的。）
 */
#ifndef HAVE_INT_OPTERR

int			opterr = 1,			/* 如果应该打印错误消息 */
			optind = 1,			/* 在父 argv 向量中的索引 */
			optopt;				/* 检查有效性的字符 */
char	   *optarg;				/* 与选项相关的参数 */

#endif

#define BADCH	(int)'?'
#define BADARG	(int)':'
#define EMSG	""

/*
 * getopt
 *	解析 argc/argv 参数向量。
 *
 * 此实现不使用 optreset。相反，我们保证在先前调用返回 -1
 * 后，可以在新的 argv 数组上重新启动，只要调用者在新系列的
 * 第一次调用之前将 optind 重置为 1。
 * （从内部来看，这意味着我们必须确保在返回 -1
 * 之前将“place”重置为 EMSG。）
 */
int getopt(int fc_nargc, char *const *fc_nargv, const char *fc_ostr)
{
	static char *fc_place = EMSG;	/* 选项字母处理 */
	char	   *fc_oli;			/* 选项字母列表索引 */

	if (!*fc_place)
	{							/* 更新扫描指针 */
		if (optind >= fc_nargc || *(fc_place = fc_nargv[optind]) != '-')
		{
			fc_place = EMSG;
			return -1;
		}
		if (fc_place[1] && *++fc_place == '-' && fc_place[1] == '\0')
		{						/* 找到 "--" */
			++optind;
			fc_place = EMSG;
			return -1;
		}
	}							/* 选项字母可以吗？ */
	if ((optopt = (int) *fc_place++) == (int) ':' ||
		!(fc_oli = strchr(fc_ostr, optopt)))
	{
		/*
		 * 如果用户没有指定 '-' 作为选项，假设它表示 -1。
		 */
		if (optopt == (int) '-')
		{
			fc_place = EMSG;
			return -1;
		}
		if (!*fc_place)
			++optind;
		if (opterr && *fc_ostr != ':')
			(void) fprintf(stderr,
						   "illegal option -- %c\n", optopt);
		return BADCH;
	}
	if (*++fc_oli != ':')
	{							/* 不需要参数 */
		optarg = NULL;
		if (!*fc_place)
			++optind;
	}
	else
	{							/* 需要一个参数 */
		if (*fc_place)				/* 无空格 */
			optarg = fc_place;
		else if (fc_nargc <= ++optind)
		{						/* 无参数 */
			fc_place = EMSG;
			if (*fc_ostr == ':')
				return BADARG;
			if (opterr)
				(void) fprintf(stderr,
							   "option requires an argument -- %c\n",
							   optopt);
			return BADCH;
		}
		else
			/* 空格 */
			optarg = fc_nargv[optind];
		fc_place = EMSG;
		++optind;
	}
	return optopt;				/* 转储回选项字母 */
}
